package com.ryder.subject.domain.handler.subject;

import com.ryder.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 题目类型工厂
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/06
 */

@Component // 添加组件 交给spring管理
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Autowired
    private List<SubjectTypeHandler> subjectTypeHandlers;

    private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    /*
     * 初始化handlerMap
     */
    public SubjectTypeHandler getHandler(int subjectType) {
        SubjectInfoTypeEnum byCode = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(byCode);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlers) {
            //枚举 加 对象
            handlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
        }
    }
}
